﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.GUI.Users.Common;
//using Portal.User.Db;
using Portal.User.Security;
using Portal.GUI.Administrator.BoxManagement.Business;

namespace Portal.GUI.Administrator.BoxManagement
{
    public partial class EditBox : System.Web.UI.UserControl
    {
        #region Members
        const string listPage = "/" + Const.OBJECT_USER + ".chn";
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            Page.RegisterHiddenField("hidIdPrefix", ClientID + "_");
            cmdSave.Attributes.Add("onclick", "return Validate();");
            if (!Page.IsPostBack)
            {
                //string id = Session["Edit_UserID"] != null ? Session["Edit_UserID"].ToString() : "";
                string id = Session["Edit_BoxID"] != null ? Session["Edit_BoxID"].ToString() : "";
                ViewState["originalId"] = id;
                ViewState["userid"] = Page.User.Identity.Name;//"admin";
                string aa = Session["Box_ID"] != null ? Session["Box_ID"].ToString() : "";
                //string a = Session["Box_ID"].ToString();
                //if (Session["Box_ID"].ToString() != null && a.Length > 0)
                if (aa != null && aa.Length > 0)
                {
                    Session["status"] = "update";
                    DataBindBox(Session["Box_ID"].ToString());
                    lblTitle.Text = "Sửa tên box";
                }
                else
                {
                    Session["status"] = "New";
                    lblTitle.Text = "Thêm mới Box";
                }
            }
            else
            {
                //SetErrorMessage(string.Empty);
            } 
        }
        #region Web Form Designer generated code

        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.cmdSave.Click += new System.EventHandler(OnSaveClick);
            this.btnCancel.Click += new System.EventHandler(OnCancelClick);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion	
        private void OnSaveClick(object sender, EventArgs e)
        {
            try
            { 
                ///Kiểm tra điều kiền trùng DL
                //Boolean b = Box.
                if (Session["status"] != "update")
                {
                    if (Box.CheckBox(txtBox.Text) == true)
                    {
                        return;
                        _errorLabel.Text = "Bản ghi bị trùng";
                    }
                    else
                    {
                        Boolean a = Box.Insert(txtBox.Text.Trim());
                        Session["Edit_BoxID"] = "";
                        _errorLabel.Text = "Lưu thành công";
                        txtBox.Text = "";
                        
                    }
                }
                else//update
                {
                    string BoxName = txtBox.Text;
                    int BoxID = Convert.ToInt32(Session["Box_ID"].ToString());
                    Boolean a = Box.UpdateBox(BoxID,BoxName);
                }
                //chuyển sang form listBox
                Session["Box_ID"]="";
                Response.Redirect("/Box/List.chn");
            } 															
            catch (Exception ex)
            {
                //SetErrorMessage(ex.Message);
            } 	
        }
        private void OnCancelClick(object sender, EventArgs e)
        {
            Session["Edit_BoxID"] = "";
            txtBox.Text = "";
            Session["Box_ID"]="";
            Response.Redirect("/Box/List.chn");
        }
        private void DataBindBox(string Box_ID)
        {
            int a = Convert.ToInt32(Box_ID);
            txtBox.Text = Box.selectBox(a);
        }

    }
}